package com.dldata.devtools30.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.script.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 呵呵 on 2018-01-23.
 */
public class ScriptUtil {
    private final static Logger logger = LoggerFactory.getLogger(ScriptUtil.class);

    public static  Object execScript(String jsdata,Object... objValueOrg){
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine scriptEngine=manager.getEngineByName("javascript");
        try {
            scriptEngine.eval(jsdata);
            Invocable invocableEngine = (Invocable) scriptEngine;
            return invocableEngine.invokeFunction("fo",objValueOrg);
        } catch (Exception e) {
            logger.error(e.getMessage()+" "+jsdata+","+objValueOrg,e);
            return null;
        }
    }

    /**
     * 返回脚本执行的result值
     * @param content
     * @return 脚本执行的result值
     */
    public final static Object evalJavaScriptResult(String content) throws ScriptException {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine scriptEngine=manager.getEngineByName("javascript");
        Object obj =null;
        try {
            logger.info("scriptcontent: "+ content);
            obj = scriptEngine.eval(content);
        } catch (Exception e) {
            logger.error(e.getMessage()+" "+content,e);
            obj=null;
            throw  e;
        }
        return obj;
    }


    public static void main(String[] args) throws ScriptException {

        String str="1112pppppppooooodgdfgdfgfgdfgsgawerarsfsfsdfsdfsdfsfsdfsfsdfsfwerwerwerww45765876887855ooouuuuu";
        System.out.println("md5: "+MD5Util.MD5Encoder(str.getBytes()));
        String toEncodeStr = Public.toEncodeStr(str.getBytes(),null);
        System.out.println("toEncodeStr: "+toEncodeStr);
        byte[] fromEncodeStr = Public.fromEncodeStr(toEncodeStr,null);
        System.out.println("md5: "+MD5Util.MD5Encoder(fromEncodeStr));


        ScriptEngineManager manager = new ScriptEngineManager();
        List<ScriptEngineFactory> factories = manager.getEngineFactories();
        for (ScriptEngineFactory factory : factories){
            System.out.println(factory.getNames());
        }
        ScriptEngine scriptEngine=manager.getEngineByName("javascript");
        scriptEngine.eval("result=/^[\\u0391-\\uFFE5]+$/.test('中文')");
        Object obj= scriptEngine.get("result");
        System.out.println("scriptEngine: "+obj);

//        getJavaScriptResult("result='111' !=null");
//        getJavaScriptResult("result=[name] !=null");
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("function fo(){").append("\r\n");
        stringBuilder.append(" var value='中文';").append("\r\n");
        stringBuilder.append(" var valueTrim=value.replace(\"/^(\\s|\\u00A0)+|(\\s|\\u00A0)+$/g\", \"\" ); ").append("\r\n");
        stringBuilder.append(" return /^[\\u0391-\\uFFE5]+$/.test(valueTrim);").append("\r\n");
        stringBuilder.append("}").append("\r\n");
        System.out.println("execScript: "+execScript(stringBuilder.toString()));

//        Pattern pattern = Pattern.compile("\\[[\\S]+\\.[\\S]+\\]");
//        Matcher matcher1=pattern.matcher("//[测试主表Demo.名称] !=null;");
        Pattern pattern = Pattern.compile("/^[\\\\u0391-\\\\uFFE5]+$/");
        Matcher matcher1=pattern.matcher("中文");
        System.out.println(matcher1.find());
//        System.out.println(matcher1.group());
//        System.out.println(matcher1.groupCount());

        String test="";
        test= test+"Math.sqrt(15) \r\n";
        test= test+"var a=68;\n";
        test= test+"if(a>10){\n";
        test= test+"    Math.sqrt(15);\n";
        test= test+"}else{\n";
        test= test+"    Math.sqrt(15);\n";
        test= test+"}";
        scriptEngine=manager.getEngineByName("javascript");
        Object obj2=scriptEngine.eval(test.toString());
        System.out.println("args = [" + obj2.getClass() + "]");
        System.out.println("args = [" + obj2 + "]");


    }
}
